﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using yncTech.BusinessObjects;
using System.Data;
using System.Collections;
using EntitySpaces.Interfaces;

public partial class Admin_NoSAPFin_NoSAPFin001 : AdminBasePage
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");
        FinBoxQuery q2 = new FinBoxQuery("q2");
        FinBoxReceiptsQuery q3 = new FinBoxReceiptsQuery("q3");

        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q5 = new TSCodeTableQuery("q5");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        TSCodeTableQuery q7 = new TSCodeTableQuery("q7");
        BFThirdUnitQuery q8 = new BFThirdUnitQuery("q8");

        q1.InnerJoin(q2).On(q1.BoxID == q2.BoxID);
        q1.InnerJoin(q3).On(q1.ReceiptID == q3.ReceiptID);
        q1.LeftJoin(q4).On(q1.FunctionDept == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageStatus == q5.Code);
        q1.LeftJoin(q6).On(q1.VoucherDescription == q6.Code);
        q1.LeftJoin(q7).On(q1.EStorageStatus == q7.Code);
        q1.LeftJoin(q8).On(q1.StorageLocation == q8.ThirdUnitID);


        q1.Select(q1.CreatedDttm, q1.ReceiptID, q2.BoxID, q2.BoxNo, q2.SupplierBoxNo, q4.Value1.As("FunctionDeptName"), q5.Value1.As("StorageStatusName"), q6.Value1.As("VoucherDescriptionName"), q8.Name, q8.CartName
            , q7.Value1.As("EStorageStatusName"), q1.FirstStorageDate, q1.CreatedBy, q1.HisDocumentEndDate, q1.HisDocumentStartDate, q1.HisIndexEnd, q1.HisIndexStart, q2.NoSapReceiptCount, q2.CompanyCode, q2.CompanyName
           , q1.StoreStartDate, q1.NeedStoreYear, q1.DestoryDate, q1.Comments, q1.EStorageLocation, q1.IsDeleted, q1.ManageUser
            );

        ArrayList args = new ArrayList();
        args.Add(q1.IsSAP == false);
        args.Add(q1.ThirdUnitNumberId.IsNull());
        

        q1.Where(args.ToArray()).OrderBy(q2.BoxNo.Ascending);

        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = e.NewPageIndex;
        pageGrid.DataBind();
    }

    #region  Delete_Command
    protected void Delete_Command(object sender, CommandEventArgs e)
    {
        decimal Id = decimal.Parse(e.CommandArgument.ToString());
        using (esTransactionScope tr = new esTransactionScope())
        {
            FinReceipt finReceipt = new FinReceipt();
            FinBox finBox = new FinBox();
            FinBoxReceipts finBoxReceipts = new FinBoxReceipts();

            finReceipt.LoadByPrimaryKey(Id);
            finReceipt.IsDeleted = true;
            decimal? boxId = finReceipt.BoxID;
            if (boxId.HasValue)
            {
                finBox.LoadByPrimaryKey(boxId.Value);
                finBox.IsDeleted = true;
            }
            FinBoxReceiptsCollection finBoxReceiptsCollection = new FinBoxReceiptsCollection();
            finBoxReceiptsCollection.Query.Where(finBoxReceiptsCollection.Query.BoxID == finBox.BoxID, finBoxReceiptsCollection.Query.ReceiptID == finReceipt.ReceiptID);
            finBoxReceiptsCollection.Query.Load();
            if (finBoxReceiptsCollection.HasData)
            {
                finBoxReceipts.LoadByPrimaryKey(finBoxReceiptsCollection[0].BoxReceiptID.Value);
                finBoxReceipts.IsDeleted = true;
            }

            finBoxReceipts.Save();
            finReceipt.Save();
            finBox.Save();
            tr.Complete();
        }

        ScriptManager.RegisterStartupScript(this, this.GetType(), Guid.NewGuid().ToString(), "showMsg()", true);

    }
    #endregion

    protected int getYear(object StoreStartDate)
    {
        int result = 0;

        if (StoreStartDate != null && IsDateTime(StoreStartDate.ToString()))
        {
            result = DateTime.Today.Year - 1 - DateTime.Parse(StoreStartDate.ToString()).Year;
            if (result < 0)
            { result = 0; }
        }

        return result;
    }


    private bool IsDateTime(string str)
    {
        DateTime temp;
        return DateTime.TryParse(str, out temp);
    }

}